/*
 * SeedPanel.java
 *
 * Created on 11 May 2006, 19:33
 */

package drayson.weboca.gui;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import org.netbeans.spi.wizard.WizardPage;



/**
 *
 * @author  Andy Roberts
 */
public class LoadCorpusPanel extends WizardPage {
    
    private JFileChooser inChooser;
    private JFileChooser outChooser;
    
    /** Creates new form SeedPanel */
    public LoadCorpusPanel() {
        super("Seeds", "Seeds");
        initComponents();
        
        // Set up some default variables
        putWizardData("CorpusSaved", "False");
        putWizardData("LoadedCorpus", " ");
        
    }
    
    public static String getDescription() {
        
        return "Seeds";
    }
    
    protected String validateContents (Component component, Object o) 
    {
                
        if (getWizardData("CorpusSaved") == "False")
        {
            if (txtCorpus.getText().trim().length() == 0) 
            {
                return "You must load a corpus for processing";
            }
            else
            {
                return "You must save your inputed corpus!";
            }
        }
        
        if (txtCorpus.getText().trim().length() == 0) 
        {
            return "You must load a corpus for processing";
        }
        
        return null;
    }
    
    private JFileChooser getInChooser() {
    
        if (inChooser == null) {
            inChooser = new JFileChooser();
        }
        
        return inChooser;
    }
    
    private JFileChooser getOutChooser() {
    
        if (outChooser == null) {
            outChooser = new JFileChooser();
        }
        
        return outChooser;
    }
    
    private void doSave()
    {
        putWizardData("CorpusSaved", "True");
        putWizardData("LoadedCorpus", txtCorpus.getText());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        btnLoad = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtCorpus = new javax.swing.JTextArea();
        saveButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        btnLoad.setText("Load Saved Corpus");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        jLabel1.setText("Paste your unprocessed corpus below, or load one using the button below:");

        txtCorpus.setColumns(20);
        txtCorpus.setRows(5);
        txtCorpus.setName("txtSeeds");
        //txtSeeds.setText("arabic\nconcordance\nlanguage\nlearning\ncluster");
        txtCorpus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCorpusKeyTyped(evt);
            }
        });

        jScrollPane1.setViewportView(txtCorpus);

        saveButton.setText("Save Corpus!");
        saveButton.setEnabled(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(btnLoad)
                        .addContainerGap())
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(jLabel1)
                            .add(34, 34, 34))
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(saveButton)
                            .addContainerGap())
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                                .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
                            .addContainerGap()))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(saveButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 172, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(10, 10, 10)
                .add(btnLoad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtCorpusKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCorpusKeyTyped
        saveButton.setEnabled(true);
        putWizardData("CorpusSaved", "False");
    }//GEN-LAST:event_txtCorpusKeyTyped

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        putWizardData("CorpusLoadChanged", "False");
        doSave();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed

        
        System.out.println("Showing the chooser");
        int returnVal = getInChooser().showOpenDialog(this);
    
        System.out.println("Approved, continuing");
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                System.out.println("trying to get the file..");
                StringBuffer x = new StringBuffer();
                
                FileReader fr = new FileReader(getInChooser().getSelectedFile()); 
                BufferedReader br = new BufferedReader(fr); 
                
                String temp;
                
                while ((temp = br.readLine()) != null)
                {
                    x.append(temp).append("\n");
                }
             
                txtCorpus.setText(x.toString());
            } catch (FileNotFoundException ex) {
                System.out.println("File not found");
                ex.printStackTrace();
            } catch (IOException ex) {
                System.out.println("Other error");
                ex.printStackTrace();
            }
        }
        System.out.println("Setting save button");
        saveButton.setEnabled(true);
        putWizardData("CorpusSaved", "False");
    }//GEN-LAST:event_btnLoadActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLoad;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton saveButton;
    protected javax.swing.JTextArea txtCorpus;
    // End of variables declaration//GEN-END:variables
    
}
